// releasePage/pages/homePageInfo/operatorList/operatorList.js
import throttle from '../../../../utils/throttle.js';
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        companyContent: [],
        operator: ['中国移动', '中国联通', '中国电信'],
        operatorValue: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getOperatorList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    choiceBtn(e) {
        let _that = this;
        let index =
            'companyContent[' + e.currentTarget.dataset.index + '].checked';
        _that.setData({
            [index]: !_that.data.companyContent[e.currentTarget.dataset.index]
                .checked,
        });
        for (let i = 0; i < _that.data.companyContent.length; i++) {
            if (_that.data.companyContent[i].checked) {
                _that.setData({
                    operatorValue: true,
                });
                return;
            } else {
                _that.setData({
                    operatorValue: false,
                });
            }
        }
    },
    // 确认
    submitBtn: throttle(function (e) {
        let _that = this;
        if (!_that.data.operatorValue) return;
        let value = '';
        let valueId = '';
        for (let i = 0; i < _that.data.companyContent.length; i++) {
            if (_that.data.companyContent[i].checked) {
                value = value + '、' + _that.data.companyContent[i].operator;
                valueId = valueId + '、' + _that.data.companyContent[i].id;
            }
        }
        let pages = getCurrentPages(); //当前页面栈
        let prevPage = pages[pages.length - 2]; //上一页面
        prevPage.setData({
            ftOperator: value.substr(1),
            ftOperatorId: valueId.substr(1),
        });
        setTimeout(function () {
            wx.navigateBack({
                delta: 1, // 返回上一级页面。
            });
        }, 300);
    }, 3100),
    // 获取运营商列表
    getOperatorList() {
        let _that = this;
        let country;
        if (app.globalData.countryBack == '中国') {
            if (app.globalData.addressBack == '香港') {
                country = '中国香港';
            } else if (
                app.globalData.taiwan.includes(app.globalData.addressBack)
            ) {
                country = '中国台湾';
            } else if (app.globalData.addressBack == '澳门') {
                country = '中国澳门';
            } else {
                country = '中国大陆';
            }
        } else {
            country = app.globalData.countryBack;
        }
        let data = {
            authId: app.globalData.authId,
            pageSize: 100,
            pageNo: 1,
            country: country,
        };
        http.getRequest(`cjMap/getCityOperatorList`, data).then((res) => {
            console.log(res);
            for (let i = 0; i < res.data.length; i++) {
                res.data[i].checked = false;
            }
            _that.setData({
                companyContent: res.data,
            });
        });
    },
});
